import { ref, toValue, watchEffect } from 'vue';
import { listToEnum, listToOption } from '@/utils/utils';
import { batchFetchDictionary } from '@/api/system';

export function useBatchDict(params: string[]) {
  const stationPropOptions = ref<Global.Option[]>([]);
  const stationPropEnum = ref<Record<string, any>>({});
  const projectInfoOptions = ref<Global.Option[]>([]);
  const projectInfoEnum = ref<Record<string, any>>({});
  const siteTypeOptions = ref<Global.Option[]>([]);
  const siteTypeEnum = ref<Record<string, any>>({});
  const stylobateTypeOptions = ref<Global.Option[]>([]);
  const stylobateTypeEnum = ref<Record<string, any>>({});
  const installEnvOptions = ref<Global.Option[]>([]);
  const installEnvEnum = ref<Record<string, any>>({});
  const noiseLevelOptions = ref<Global.Option[]>([]);
  const noiseLevelEnum = ref<Record<string, any>>({});
  const provinceOptions = ref<Global.Option[]>([]);
  const provinceEnum = ref<Record<string, any>>({});

  const fetchData = () => {
    batchFetchDictionary(toValue(params)).then((res: any) => {
      const {
        station_prop: stationProp,
        project_info: projectInfo,
        site_type: siteType,
        stylobate_type: stylobateType,
        install_env: installEnv,
        noise_level: noiseLevel,
        province_data: provinceData,
      } = res.data;
      stationPropOptions.value = listToOption(
        stationProp,
        'dictLabel',
        'dictValue',
      );
      stationPropEnum.value = listToEnum(stationProp, 'dictValue', 'dictLabel');

      projectInfoOptions.value = listToOption(
        projectInfo,
        'dictLabel',
        'dictValue',
      );
      projectInfoEnum.value = listToEnum(projectInfo, 'dictValue', 'dictLabel');

      siteTypeOptions.value = listToOption(siteType, 'dictLabel', 'dictValue');
      siteTypeEnum.value = listToEnum(siteType, 'dictValue', 'dictLabel');

      stylobateTypeOptions.value = listToOption(
        stylobateType,
        'dictLabel',
        'dictValue',
      );
      stylobateTypeEnum.value = listToEnum(
        stylobateType,
        'dictValue',
        'dictLabel',
      );

      installEnvOptions.value = listToOption(
        installEnv,
        'dictLabel',
        'dictValue',
      );
      installEnvEnum.value = listToEnum(installEnv, 'dictValue', 'dictLabel');

      noiseLevelOptions.value = listToOption(
        noiseLevel,
        'dictLabel',
        'dictValue',
      );
      noiseLevelEnum.value = listToEnum(noiseLevel, 'dictValue', 'dictLabel');

      provinceOptions.value = listToOption(
        provinceData,
        'dictLabel',
        'dictLabel',
      );
      provinceEnum.value = listToEnum(provinceData, 'dictValue', 'dictLabel');
    });
  };

  fetchData();

  return {
    stationPropOptions,
    stationPropEnum,
    projectInfoOptions,
    projectInfoEnum,
    siteTypeOptions,
    siteTypeEnum,
    stylobateTypeOptions,
    stylobateTypeEnum,
    installEnvOptions,
    installEnvEnum,
    noiseLevelOptions,
    noiseLevelEnum,
    provinceOptions,
    provinceEnum,
  };
}
